package com.huawei123.hasee.cniao5store.mvp.view.acyivity;

import android.os.Build;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.huawei123.hasee.cniao5store.R;
import com.huawei123.hasee.cniao5store.adapter.FixPagerAdapter;
import com.huawei123.hasee.cniao5store.base.BaseActivity;
import com.huawei123.hasee.cniao5store.base.BaseFragment;
import com.huawei123.hasee.cniao5store.factory.FragmentFactory;
import com.huawei123.hasee.cniao5store.utils.LayoutUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class HomeActivity extends BaseActivity {
    @BindView(R.id.mV_title_margin)
    View mV_title_margin;
    @BindView(R.id.mTL_select)
    TabLayout mTL_select;
    @BindView(R.id.mVP_main_viewpager)
    ViewPager mVP_main_viewpager;
    private FixPagerAdapter fixPagerAdapter;
    private String[] titles = {"推荐", "分类", "排行", "管理", "我的"};

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_home);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        mV_title_margin.post(new Runnable() {
            @Override
            public void run() {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                     /*得到状态栏高度*/
                    int statusBarHeight = LayoutUtils.getStatusBarHeight(HomeActivity.this);
                    if (statusBarHeight > 0) {
                        android.widget.LinearLayout.LayoutParams layoutParams = (android.widget.LinearLayout.LayoutParams)
                                mV_title_margin.getLayoutParams();
                        layoutParams.height = statusBarHeight;
                        mV_title_margin.setLayoutParams(layoutParams);
                    }
                }
            }
        });
        /*实例化Tablayout与ViewPager*/
        initViewPagerForgment();
    }

    private void initViewPagerForgment() {
        fixPagerAdapter = new FixPagerAdapter(getSupportFragmentManager());//初始化ViewPager适配器
        /*初始化fragment放入集合中*/
        List<Fragment> fragments = new ArrayList<>();
        for (int i = 0; i < titles.length; i++) {
            fragments.add(FragmentFactory.createFragment(i));
        }
        /*初始化viewPager适配器*/
        fixPagerAdapter.setTitle(titles);
        fixPagerAdapter.setFragments(fragments);
        mVP_main_viewpager.setAdapter(fixPagerAdapter);
        /*Tablayout绑定ViewPager*/
        mTL_select.setupWithViewPager(mVP_main_viewpager);
        /*Tablayout为固定模式*/
//        mTL_select.setTabMode(TabLayout.MODE_FIXED);

        mVP_main_viewpager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                BaseFragment fragment = FragmentFactory.createFragment(position);
                fragment.show();
            }
        });
    }
}
